package com.mingyizhudao.app.view.recommend;

import java.util.Map;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dixiang.framework.common.QJNetUICallback;
import com.dixiang.framework.common.QjResult;
import com.dixiang.framework.widget.LoadingDialog;
import com.google.gson.reflect.TypeToken;
import com.mingyizhudao.app.R;
import com.mingyizhudao.app.entity.DeptEntity;
import com.mingyizhudao.app.utils.CommonUtils;
import com.mingyizhudao.app.utils.Constants;
import com.mingyizhudao.app.view.MYBaseActivity;

public class DeptIntroduceActivity extends MYBaseActivity implements
		OnClickListener {
	private String deptUrl, deptId, facultyName, hospitalName;

	private TextView tv_dept_1,tv_dept_2,tv_dept_3,tv_dept_4;
	private LinearLayout ll_honour;
	private Button btn_order;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_dept_introduce);
		super.onCreate(savedInstanceState);
		initView();
	}

	@Override
	public void initHeaderView() {

		initBackView(R.drawable.left_back_white_icon, 0, new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private void initView() {
		
		hospitalName = getIntent().getStringExtra("hospitalName");
		deptUrl = getIntent().getStringExtra("deptUrl");
		facultyName = getIntent().getStringExtra("facultyName");
		deptId = getIntent().getStringExtra("deptId");
		setTitleViewValue(0, 0, R.color.white);

		btn_order = (Button) findViewById(R.id.btn_order);
		
		tv_dept_1 = (TextView) findViewById(R.id.tv_dept_1);
		tv_dept_2 = (TextView) findViewById(R.id.tv_dept_2);
		tv_dept_3 = (TextView) findViewById(R.id.tv_dept_3);
		tv_dept_4 = (TextView) findViewById(R.id.tv_dept_4);
		ll_honour = (LinearLayout) findViewById(R.id.ll_honour);
		
		
		
		setTitleViewValue(facultyName);
		btn_order.setOnClickListener(this);

		getDeptIntro();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_order:
			CommonUtils.goToCreateOrder(activity, deptId, null, facultyName,
					hospitalName, null, true);
			break;
		default:
			break;
		}
	}

	private void getDeptIntro() {
		LoadingDialog.getInstance(activity).show();
		getHttpResult(deptUrl, Constants.HTTP_INDEX.First, new TypeToken<QjResult<Map<String, DeptEntity>>>() {});
	}
	
	@Override
	public void onSuccess(QjResult result, String flg) {
		super.onSuccess(result, flg);
		Map<String, DeptEntity> map = (Map<String, DeptEntity>) result.getResults();
		if (map == null)
			return;
		DeptEntity deptEntity = map.get("department");
		if (deptEntity != null) {
			//setTitleViewValue(deptEntity.getHpName());
			setTitleViewValue(deptEntity.getName());
//			String des = deptEntity.getDescription();
//			if (des != null && des.length() > 0) {
//				dept_introduce.setText("科室介绍："+ deptEntity.getDescription());
//			} else {
//				dept_introduce.setText("暂无介绍");
//			}
			
			String position = deptEntity.getPosition();
			if (position != null && position.length() > 0) {
				tv_dept_1.setText( position);
			}else {
				tv_dept_1.setText("暂无明确信息。");
			}
			
			String scale = deptEntity.getScale();
			if (scale != null && scale.length() > 0) {
				tv_dept_2.setText( scale);
			}else {
				tv_dept_2.setText("暂无明确信息。");
			}
			
			String specialty = deptEntity.getSpecialty();
			if (specialty != null && specialty.length() > 0) {
				tv_dept_3.setText( specialty);
			}else {
				tv_dept_3.setText("暂无明确信息。");
			}
			
			String strength = deptEntity.getStrength();
			if (strength != null && strength.length() > 0) {
				tv_dept_4.setText( strength);
			}else {
				tv_dept_4.setText("暂无明确信息。");
			}
			
			//tv_dept_5.setText( deptEntity.getStrength());
			
			String honour[] = deptEntity.getHonor();
			if (honour != null && honour.length > 0) {
				ll_honour.setVisibility(View.VISIBLE);

				for (int i = 0; i < honour.length; i++) {

					View contentView = getLayoutInflater().inflate(R.layout.view_xueke_honor, null);

					ImageView iv_num = (ImageView) contentView.findViewById(R.id.iv_num);
					TextView tv_num = (TextView) contentView.findViewById(R.id.tv_num);
					iv_num.setImageResource(R.drawable.iv_hei_dian);
					tv_num.setText(honour[i]);
					ll_honour.addView(contentView);
				}

			} else {
				ll_honour.setVisibility(View.GONE);
			}

		} else {
			Toast.makeText(activity, "获取数据失败！", 0).show();
		}
	}
	
	@Override
	public void onCompleted(Exception e, String flg) {
		super.onCompleted(e, flg);
		LoadingDialog.getInstance(activity).dismiss();
	}

}
